/**
 * 提交回复
 */
function post() {
    var questionId = $("#question_id").val();
    var content = $("#comment_content").val();
    commentTarget(questionId, 1, content);
}

/**
 * 封装评论方法
 */
function commentTarget(targetId, type, content) {
    //content有值时为true
    if (!content) {
        alert("不能回复空内容~");
        return;
    }

    $.ajax({
        type: "POST",
        url: "/comment",
        contentType: "application/json",
        data: JSON.stringify({
            "parentId": targetId,
            "content": content,
            "type": type
        }),
        success: function (response) {
            if (response.code == 200) {
                //隐藏回复输入框
                //$("#comment_section").hide();
                //刷新页面
                window.location.reload();
            } else {
                if (response.code == 2003) {
                    var isAccepted = confirm(response.message);//confirm()弹窗选确定返回true
                    if (isAccepted) {
                        //window.open()打开一个新地址
                        //window.open("https://github.com/login/oauth/authorize?client_id=0c6d66e67b1cf765704c&redirect_uri=http://localhost:8080/callback&scope=user&state=1");
                        window.open("https://gitee.com/oauth/authorize?client_id=b641fea85ec0a2397e43a0e92bf82c94ab42003659bb9cceaa5636c9a058097f&redirect_uri=http://localhost:8080/callback/gitee&response_type=code");
                        //localStorage为Html5的存储，可以在本地存储用户的浏览数据
                        window.localStorage.setItem("closable", true);
                        //使用模态框方式。现在无法不跳转到首页
                        //$('#myModal').modal({})
                    } else {
                        alert(response.message);
                    }
                }
            }
        },
        dataType: "json"
    });
}

/**
 * 提交评论
 */
function comment(e) {
    var commnetId = e.getAttribute("data-id");
    var content = $("#input-" + commnetId).val();//获取内容
    commentTarget(commnetId, 2, content);
}

/**
 * 展开折叠二级评论
 */
function collapseComments(e) {
    debugger;
    var id = e.getAttribute("data-id");
    let comments = $("#comment-" + id);//获取对象

    //获取二级评论的展开状态
    var collapse = e.getAttribute("data-collapse");
    if (collapse) {
        //折叠二级评论
        comments.removeClass("in");
        e.removeAttribute("data-collapse");
        e.classList.remove("active");
    } else {
        //拼接页面，显示二级评论
        var subCommentContainer = $("#comment-" + id);
        if (subCommentContainer.children().length != 1) {//判断子元素
            //展开二级评论
            comments.addClass("in");
            //标记二级评论展开状态
            e.setAttribute("data-collapse", "in");
            e.classList.add("active");
        } else {
            $.getJSON("/comment/" + id, function (data) {
                $.each(data.data.reverse(), function (index, comment) {
                    var mediaLeftElement = $("<div/>", {
                        "class": "media-left"
                    }).append($("<img/>", {
                        "class": "media-object img-rounded",
                        "src": comment.user.avatarUrl
                    }));

                    var mediaBodyElement = $("<div/>", {
                        "class": "media-body",
                    }).append($("<h5/>", {
                        "class": "media-heading",
                        "html": comment.user.name
                    })).append($("<div/>", {
                        "html": comment.content
                    })).append($("<div/>", {
                        "class": "menu"
                    }).append($("<span/>", {
                        "class": "pull-right",
                        "html": moment(comment.gmtCreate).format('YYYY-MM-DD hh:mm')
                    })));

                    var mediaElement = $("<div/>", {
                        "class": "media",
                    }).append(mediaLeftElement).append(mediaBodyElement);

                    var commentElement = $("<div/>", {
                        "class": "col-lg-12 col-md-12 col-sm-12 col-xs-12 comments"
                    }).append(mediaElement);
                    subCommentContainer.prepend(commentElement);
                });
                //展开二级评论
                comments.addClass("in");
                //标记二级评论展开状态
                e.setAttribute("data-collapse", "in");
                e.classList.add("active");
            });
        }
    }
}

/**
 * 展示标签
 */
function showSelectTab() {
    $("#select-tag").show();
}

/**
 * 发布问题选择标签
 */
function selectTag(e) {
    var value = e.getAttribute("data-tag");
    var previous = $("#tag").val();
    console.log(previous);

    //判断该值是否存在，不存在添加
    if (previous.indexOf(value) == -1) {
        if (previous) {
            $("#tag").val(previous + ',' + value);
        } else {
            $("#tag").val(value);
        }
    }
}